package plantshop.model;
import java.io.Serializable;
import java.util.Arrays;

import plantshop.model.exceptions.ImproperLengthException;



public class Characteristics implements Serializable{
	private static final long serialVersionUID = 3650246416458969143L;
	
	/*
	 * 0 - Butterflies
	 * 1 - Dense Roots
	 * 2 - Provide Shade
	 * 3 - Nourish Wildlife
	 * 4 - Engage People
	 */
	
	private int[] chars;
	private int dominant;
	
	private final static int THRESHOLD = 1;
	
	public Characteristics(int...chars){
		this.chars = chars;
		findDominant();
	}
	
	public double getCloseness(int...chars) throws ImproperLengthException{
		double total = 0;
		if (this.chars.length != chars.length){
			throw new ImproperLengthException();
		}
		for(int i=0; i<chars.length; i++)
			total += Math.pow(chars[i]-this.chars[i], 2);
		return total;
	}
	
	public double getCloseness(Characteristics chars) throws ImproperLengthException{
		return getCloseness(chars.chars);
	}
	
	private void findDominant(){
		int max = 0;
		int second = 0;
		int pos = 0;
		for(int i = 0;i<chars.length;i++){
			if(chars[i] > second){
				if(chars[i] > max)
				{
					second = max;
					max = chars[i];
					pos = i;
				}
				else
					second = chars[i];
			}
		}
		
		if(max-second >= THRESHOLD){
			dominant = pos;
		}
		else{
			dominant = -1;
		}
	}
	
	public int getDominant(){
		return dominant;
	}
	
	@Override
	public String toString() {
		return Arrays.toString(chars);
	}

	@Override
	public boolean equals(Object o){
		if(!(o instanceof Characteristics))
			return false;
		int[] other = ((Characteristics)o).chars;
		if(other.length!=chars.length)
			return false;
		for(int i = 0;i<chars.length;i++){
			if(chars[i]!=other[i])
				return false;
		}
		return true;
	}
	
	@Override
	public int hashCode(){
		return chars.hashCode();
	}
	
	public int[] getChars()
	{
		return chars;
	}
}
